package com.example.imitation_webchat_demo.model;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.example.imitation_webchat_demo.R;

// 自定义的二级下拉列表，省市联动下拉列表
public class TwoLevelSpinner extends LinearLayout {
	private Spinner provinceSpinner;
	private Spinner citySpinner;
	private String province;
	private String city;
	private ArrayAdapter<CharSequence> cityAdapter;

	public TwoLevelSpinner(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public TwoLevelSpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public TwoLevelSpinner(Context context) {
		super(context);
		init(context);
	}

	public void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.two_level_spinner, this);
		loadSpinner();
	}

	public void loadSpinner() {
		provinceSpinner = (Spinner) findViewById(R.id.two_level_province_spinner);
		citySpinner = (Spinner) findViewById(R.id.two_level_city_spinner);
		ArrayAdapter<CharSequence> provinceAdapter = ArrayAdapter
				.createFromResource(this.getContext(), R.array.province,
						android.R.layout.simple_spinner_item);
		provinceAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		provinceSpinner.setAdapter(provinceAdapter);

		provinceSpinner
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						// parent既是province对象
						Spinner spinner = (Spinner) parent;
						province = (String) spinner.getItemAtPosition(position);
						setCityAdapter();
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent) {

					}
				});

		citySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				Spinner spinner = (Spinner) arg0;
				city = (String) spinner.getItemAtPosition(arg2);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	public void setCityAdapter() {
		// (处理省的市的显示)
		// 将默认值与ArrayAdapter连接(从资源数组文件中获取数据)
		cityAdapter = ArrayAdapter.createFromResource(
				TwoLevelSpinner.this.getContext(), R.array.beijing,
				android.R.layout.simple_spinner_item);
		// new ArrayAdapter<CharSequence>
		// (MainActivity.this,android.R.layout.simple_spinner_item,
		// cities);
		// 获取所在省含有哪些市(从资源数组文件中获取数据)
		if (province.equals("北京市")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.beijing,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("天津市")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.tianjin,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("上海市")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.shanghai,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("重庆市")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.chongqing,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("河北省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.hebei,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("山西省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.shanxi,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("内蒙古自治区")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.neimenggu,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("辽宁省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.liaoning,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("吉林省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.jilin,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("黑龙江省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.heilongjiang,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("江苏省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.jiangsu,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("浙江省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.zhejiang,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("安徽省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.anhui,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("福建省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.fujian,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("江西省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.jiangxi,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("山东省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.shandong,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("河南省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.henan,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("湖北省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.hubei,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("湖南省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.hunan,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("广东省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.guangdong,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("广西壮族自治区")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.guangxi,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("海南省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.hainan,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("四川省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.sichuan,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("贵州省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.guizhou,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("云南省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.yunnan,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("西藏自治区")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.xizang,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("陕西省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.shanxi_,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("甘肃省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.gansu,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("青海省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.qinghai,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("宁夏回族自治区")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.ningxia,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("新疆维吾尔自治区")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.xinjiang,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("香港特别行政区")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.hongkong,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("澳门特别行政区")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.macro,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("台湾省")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.taiwan,
					android.R.layout.simple_spinner_item);
		} else if (province.equals("请选择省份")) {
			cityAdapter = ArrayAdapter.createFromResource(
					TwoLevelSpinner.this.getContext(), R.array.choose_nothing,
					android.R.layout.simple_spinner_item);
		}
		cityAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 绑定数据到Spinner(City)上
		citySpinner.setAdapter(cityAdapter);
	}

	public void setProvinceSpinner(int id) {
		provinceSpinner.setSelection(id);
	}

	public void setCitySpinner(int id) {
		citySpinner.setSelection(id);
	}

	public Spinner getProvinceSpinner() {
		return provinceSpinner;
	}

	public Spinner getCitySpinner() {
		return citySpinner;
	}

	public String getProvince() {
		return province;
	}

	public String getCity() {
		return city;
	}

	public int getProvinceId() {
		return (int) provinceSpinner.getSelectedItemId();
	}

	public int getCityId() {
		return (int) citySpinner.getSelectedItemId();
	}

	public void setProvinceId(int position) {
		provinceSpinner.setSelection(position, true);
		province = (String) provinceSpinner.getSelectedItem();
		setCityAdapter();
		cityAdapter.notifyDataSetChanged();
	}

	public void setCityId(int position) {
		citySpinner.setSelection(position, true);
	}
}